package czt.ocr.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import czt.ocr.entity.EFile;

import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class EFileExcelWriter {

    public static void writeEFilesToExcel(List<EFile> eFiles, String templateFileName, String outputFileName) {
        try (InputStream templateInputStream = EFileExcelWriter.class.getClassLoader().getResourceAsStream(templateFileName);
             OutputStream outputStream = new FileOutputStream(outputFileName)) {

            if (templateInputStream == null) {
                throw new IllegalArgumentException("模板文件未找到: " + templateFileName);
            }

            // 创建 ExcelWriterBuilder
            ExcelWriterBuilder writerBuilder = EasyExcel.write(outputStream).withTemplate(templateInputStream);

            // 创建 WriteSheet
            WriteSheet writeSheet = EasyExcel.writerSheet().build();

            // 创建 FillConfig
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

            // 填充数据
            writerBuilder.build().fill(eFiles, fillConfig, writeSheet).finish();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
